package com.cloud.bssp.java8.stream;

import java.util.stream.LongStream;

/**
 * @description： 并发情况下的流
 * @author：weirx
 * @date：2021/10/25 11:02
 * @version：3.0
 */
public class ConcurrentForStreamParallel {

    /**
     * description: 调用Accumulator的add方法求和
     * @param n
     * @return: long
     * @author: weirx
     * @time: 2021/10/25 11:03
     */
    public static long sideEffectSum(long n) {
        Accumulator accumulator = new Accumulator();
        LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
        return accumulator.total;
    }

    public static class Accumulator {
        public long total = 0;

        public void add(long value) {
            total += value;
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i< 10 ;i++){
            System.out.println(sideEffectSum(10000000));
        }
    }
}
